﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RapChieuPhim.DTO;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using RapChieuPhim.DAO;
using RapChieuPhim.BUS;

namespace RapChieuPhim.DAO
{
    class TaiKhoanDAO
    {
        public TaiKhoanDTO GetByID(int id)
        {
            DataProvider cn = new DataProvider();
            SqlParameter param = new SqlParameter("@MaTaiKhoan", id);
            DataTable dt = cn.SqlExcuteQuery("LayTaiKhoanTheoID", param);
            TaiKhoanDTO TaiKhoan = new TaiKhoanDTO();

            if (dt.Rows.Count != 0)
            {
                TaiKhoan.MaTaiKhoan = int.Parse(dt.Rows[0]["MaTaiKhoan"].ToString());
                TaiKhoan.UserName = dt.Rows[0]["Username"].ToString();
                TaiKhoan.Pass = dt.Rows[0]["Pass"].ToString();
                TaiKhoan.NhanVien = NhanVienBUS.GetByID(dt.Rows[0]["MaNV"].ToString());
            }
            return TaiKhoan;
        }
        public TaiKhoanDTO GetByUser(string username)
        {
            DataProvider cn = new DataProvider();
            SqlParameter param = new SqlParameter("@UserName", username);
            DataTable dt = cn.SqlExcuteQuery("LayTaiKhoanTheoUserName", param);
            TaiKhoanDTO TaiKhoan = new TaiKhoanDTO();

            if (dt.Rows.Count != 0)
            {
                TaiKhoan.MaTaiKhoan = int.Parse(dt.Rows[0]["MaTaiKhoan"].ToString());
                TaiKhoan.UserName = dt.Rows[0]["Username"].ToString();
                TaiKhoan.Pass = dt.Rows[0]["Pass"].ToString();
                TaiKhoan.NhanVien = NhanVienBUS.GetByID(dt.Rows[0]["MaNV"].ToString());
            }
            return TaiKhoan;
        }
        public IList GetAll()
        {
            DataProvider cn = new DataProvider();
            ArrayList array = new ArrayList();
            DataTable dt = cn.SqlExcuteQuery("LayAllTaiKhoan");

            if (dt.Rows.Count != 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    TaiKhoanDTO TaiKhoan = new TaiKhoanDTO();
                    TaiKhoan.MaTaiKhoan = int.Parse(dr["MaTaiKhoan"].ToString());
                    TaiKhoan.UserName = dr["Username"].ToString();
                    TaiKhoan.Pass = dr["Pass"].ToString();
                    TaiKhoan.NhanVien = NhanVienBUS.GetByID(dr["MaNV"].ToString());
                    array.Add(TaiKhoan);
                }
            }
            return array;
        }

        public void Insert(TaiKhoanDTO TaiKhoan)
        {
            DataProvider cn = new DataProvider();
            SqlParameter[] param = new SqlParameter[3];
            param[0] = new SqlParameter("@Username", TaiKhoan.UserName);
            param[1] = new SqlParameter("@Pass", TaiKhoan.Pass);
            param[2] = new SqlParameter("@MaNV", TaiKhoan.NhanVien);
            cn.SqlExcuteNonQuery("InsertTaiKhoan", param);
        }

        public void Update(TaiKhoanDTO TaiKhoan)
        {
            DataProvider cn = new DataProvider();
            SqlParameter[] param = new SqlParameter[4];
            param[0] = new SqlParameter("@MaTaiKhoan", TaiKhoan.MaTaiKhoan);
            param[1] = new SqlParameter("@Username", TaiKhoan.UserName);
            param[2] = new SqlParameter("@Pass", TaiKhoan.Pass);
            param[3] = new SqlParameter("@MaNV", TaiKhoan.NhanVien.MaNhanVien);
            cn.SqlExcuteNonQuery("UpdateTaiKhoan", param);
        }

        public void Delete(int id)
        {
            DataProvider cn = new DataProvider();
            SqlParameter param = new SqlParameter("@MaTaiKhoan", id);
            cn.SqlExcuteNonQuery("DeleteTaiKhoan", param);
        }
    }
}
